package cn.tkb.org.controller;

import cn.tkb.basic.util.AjaxResult;
import cn.tkb.basic.util.PageQuery;
import cn.tkb.org.domain.DictionaryDetail;
import cn.tkb.org.domain.DictionaryType;
import cn.tkb.org.query.DictionaryTypeQuery;
import cn.tkb.org.service.IDictionaryTypeService;
import cn.tkb.org.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dict")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;
    //新增和修改
    @RequestMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if (dictionaryType.getId() == null){
                dictionaryTypeService.add(dictionaryType);
            }else {
                dictionaryTypeService.update(dictionaryType);
            }
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败！！！");
        }
    }
    //查询部门
    @GetMapping("/{id}")
    public DictionaryType queryById(@PathVariable("id") Long id){
        return dictionaryTypeService.QeuryById(id);
    }
    //查询详情
    @PostMapping("/{id}")
    public List<DictionaryDetail> queryByDictId(@PathVariable("id") Long id){
        return dictionaryTypeService.queryByDictId(id);
    }
    //删除部门
    @DeleteMapping("/{id}")
        public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            dictionaryTypeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }

    }
    //批量删除
    @PatchMapping
    public AjaxResult BatchDelete(@RequestBody List<Long> ids){
        try {
            dictionaryTypeService.BatchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.fail(e.getMessage());
        }
    }
    //查询所有部门
    @GetMapping
    public List<DictionaryType> queryAll(){
        return dictionaryTypeService.QeuryAll();
    }
    //高级查询和分页查询
    @PostMapping
    public PageQuery<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery query){
       return dictionaryTypeService.findPage(query) ;
    }
}
